package com.cloudeasy.label.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.label.LabelVO;
import com.cloudeasy.api.vo.user.UserInfoVO;
import com.cloudeasy.core.bean.JwtParser;
import com.cloudeasy.label.client.UserClient;
import com.cloudeasy.label.po.LabelAttention;
import com.cloudeasy.label.service.LabelAttentionService;
import com.cloudeasy.label.service.LabelService;
import com.cloudeasy.label.util.LabelUtil;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/attention")
public class LabelAttentionController {
    private final LabelService labelService;

    private final LabelAttentionService labelAttentionService;

    private final UserClient userClient;

    private final JwtParser jwtParser;

    public LabelAttentionController(LabelService labelService, LabelAttentionService labelAttentionService, UserClient userClient, JwtParser jwtParser) {
        this.labelService = labelService;
        this.labelAttentionService = labelAttentionService;
        this.userClient = userClient;
        this.jwtParser = jwtParser;
    }

    /**
     * 关注
     *
     * @param authentication {@link Authentication}
     * @param id             Id
     * @return {@link Result<Boolean>}
     */
    @PostMapping("/{id}")
    public Result<Boolean> attention(Authentication authentication, @PathVariable("id") String id) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        return ResultUtil.success(labelAttentionService.attention(userId, id));
    }

    /**
     * 取消关注
     *
     * @param authentication {@link Authentication}
     * @param id             Id
     * @return {@link Result<Boolean>}
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> removeAttention(Authentication authentication, @PathVariable("id") String id) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        return ResultUtil.success(labelAttentionService.removeAttention(userId, id));
    }

    /**
     * 获取登录用户的关注信息
     *
     * @param authentication {@link Authentication}
     * @return {@link List<LabelVO>}的{@link Result}
     */
    @GetMapping
    public Result<List<LabelVO>> myAttention(Authentication authentication) {
        List<String> labelIds = labelAttentionService.listByUserId((String) jwtParser.getOnBody(authentication, "userId"))
                .stream().map(LabelAttention::getLabelId).distinct().collect(Collectors.toList());
        if (labelIds.size() == 0) {
            return ResultUtil.success(new ArrayList<>());
        }
        return ResultUtil.success(LabelUtil.convert(new ArrayList<>(labelService.listByIds(labelIds))));
    }

    /**
     * 获取某用户关注的标签
     *
     * @param userId 用户Id
     * @return {@link List<LabelVO>}的{@link Result}
     */
    @GetMapping("/user/{userId}")
    public Result<List<LabelVO>> getAttention(@PathVariable("userId") String userId) {
        List<String> labelIds = labelAttentionService.listByUserId(userId)
                .stream().map(LabelAttention::getLabelId).distinct().collect(Collectors.toList());
        if (labelIds.size() == 0) {
            return ResultUtil.success(new ArrayList<>());
        }
        return ResultUtil.success(LabelUtil.convert(new ArrayList<>(labelService.listByIds(labelIds))));
    }

    /**
     * 查询关注了标签的用户
     *
     * @param id   Id
     * @param asc  正序排序字段
     * @param desc 倒序排序字段
     * @return {@link List<UserInfoVO>}的{@link Result}
     */
    @GetMapping("/{id}")
    public Result<List<UserInfoVO>> list(@PathVariable("id") String id) {
        List<String> userIds = labelAttentionService.listById(id).stream().map(LabelAttention::getUserId).distinct().collect(Collectors.toList());
        if (userIds.size() == 0) {
            return ResultUtil.success(new ArrayList<>());
        }
        return userClient.list(userIds, null, null, null);
    }

    /**
     * 分页查询关注了标签的用户
     *
     * @param id   标签Id
     * @param page 页数
     * @param size 每页大小
     * @return {@link PageResult<UserInfoVO>}的{@link Result}
     */
    @GetMapping("/{id}/{page}/{size}")
    public Result<PageResult<UserInfoVO>> page(@PathVariable("id") String id,
                                               @PathVariable("page") Integer page,
                                               @PathVariable("size") Integer size) {
        IPage<LabelAttention> labelAttentionIPage = labelAttentionService.pageById(id, page, size);
        List<String> userIds = labelAttentionIPage.getRecords().stream().map(LabelAttention::getUserId).distinct().collect(Collectors.toList());
        if (userIds.size() == 0) {
            PageResult<UserInfoVO> pageResult = PageResult.newInstance(labelAttentionIPage.getTotal(), labelAttentionIPage.getPages());
            return ResultUtil.success(pageResult);
        }
        return userClient.page(userIds, null, null, null, page, size);
    }
}
